package com.jiejie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiejie.commons.ServerResponse;
import com.jiejie.entity.Comments;
import com.jiejie.entity.Customer;
import com.jiejie.mapper.CommentsMapper;
import com.jiejie.mapper.CustomerMapper;
import com.jiejie.service.ICommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@Service
public class CommentsServiceImpl  implements ICommentsService {

    @Resource
    private CommentsMapper commentsMapper;
    @Resource
    private CustomerMapper customerMapper;
    @Override
    public Map<Comments, Customer> getCommentsByMovieId(Integer movieId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("movie_id",movieId);
        queryWrapper.eq("comments_status",0);
        queryWrapper.orderByDesc("comments_id");
        //评论id,电影id,用户id,电影评分,评论内容,评论点赞数,评论时间
        queryWrapper.select("comments_id","movie_id","customer_id","comments_score","comments_content","comments_likes","comments_create_time");
        List<Comments> commentsList=commentsMapper.selectList(queryWrapper);
        System.out.println("==========");
        System.out.println(commentsList);
        Map<Comments,Customer> commentsCustomerMap=new LinkedHashMap<>();
        for(int i=0;i<commentsList.size();i++){
            Customer customer=customerMapper.selectById(commentsList.get(i).getCustomerId());
            commentsCustomerMap.put(commentsList.get(i),customer);
        }
        return commentsCustomerMap;
    }

    @Override
    public Float getAllCommentsScore(Integer movieId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("movie_id",movieId);
        queryWrapper.eq("comments_version",0);
        queryWrapper.select("comments_score");
        List<Comments> commentsList=commentsMapper.selectList(queryWrapper);
        float allCommentsScore=0.0f;
        if(commentsList.size()>0){
            for (Comments c:commentsList) {
                allCommentsScore+=Float.parseFloat(c.getCommentsScore());
            }
            allCommentsScore=allCommentsScore/commentsList.size();
        }

        allCommentsScore=allCommentsScore/commentsList.size();
        System.out.println(">>>>>>>>>>>>>"+allCommentsScore);
        return allCommentsScore;
    }

    @Override
    public ServerResponse saveComment(Comments comments) {
        int rows=commentsMapper.insert(comments);
        return ServerResponse.success("评论成功",rows);
    }
    @Override
    public List<Comments> getCommentsByMovieIdForXQ(Integer movieId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("movie_id",movieId);
        queryWrapper.eq("comments_status",0);
        queryWrapper.orderByDesc("comments_id");
        //评论id,电影id,用户id,电影评分,评论内容,评论点赞数,评论时间
        queryWrapper.select("comments_id","movie_id","customer_id","comments_score","comments_content","comments_likes","comments_create_time");
        List<Comments> commentsList=commentsMapper.selectList(queryWrapper);
        System.out.println("==========");
        System.out.println(commentsList);

        return commentsList;
    }
}
